/**
* @file			arduino.c
* @author  		Supachai Vorapojpisut
* @version 		0.2
* @date    		November 11, 2013
* @brief   		Arduino compatibility module - add digitalRead()
*/

/* Includes ------------------------------------------------------------------*/
#include "arduino.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define digitalPinToPort(P) 	( DIGITAL_PIN_TO_PORT[P] )
#define digitalPinToBitMask(P) 	( DIGITAL_PIN_TO_BIT_MASK[P] )

/* Private variables ---------------------------------------------------------*/
GPIO_TypeDef *DIGITAL_PIN_TO_PORT[14] = {
uint16_t DIGITAL_PIN_TO_BIT_MASK[14] = {

static uint32_t msTick = 0;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

// SysTick handler to implement timebase features.
void SysTick_Handler()
{
	msTick++;
}


/**
* @brief	Initialization code to configure STM32F0 Discovery board for Arduino compatibility
* @param	None
* @retval	None
*/
void platform_config()
{
	RCC_AHBPeriphClockCmd( , );
	SysTick_Config( );
}


/**
* @brief	Configures the specified pin to behave either as an input or an output. 
* @param	pin		the number of the pin whose mode you wish to set
* @param	mode	INPUT or OUTPUT
* @retval	None
*/
void pinMode(uint8_t pin, int mode)
{
	GPIO_InitTypeDef gpio_init_struct;
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	
	if (mode == OUTPUT) {
		gpio_init_struct.GPIO_Mode = GPIO_Mode_OUT;
		gpio_init_struct.GPIO_OType = GPIO_OType_PP;
		gpio_init_struct.GPIO_Speed = GPIO_Speed_Level_3;
		gpio_init_struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
		gpio_init_struct.GPIO_Pin = gpio_pin;
	} else {
		gpio_init_struct.GPIO_Mode = GPIO_Mode_IN;
		gpio_init_struct.GPIO_PuPd = GPIO_PuPd_UP;
		gpio_init_struct.GPIO_Pin = gpio_pin;		
	}
	GPIO_Init(gpio_port, &gpio_init_struct);
}


/**
* @brief	Reads the value from a specified digital pin, either HIGH or LOW.
* @param	pin		the number of the digital pin you want to read
* @retval	HIGH or LOW
*/
int digitalRead(uint8_t pin)
{
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	
	if ( ) {
		return HIGH;
	}
	return LOW;
}


/**
* @brief	Write a HIGH or a LOW value to a digital pin.
* @param	pin		the pin number
* @param	value	HIGH or LOW
* @retval	None
*/
void digitalWrite(uint8_t pin, int value)
{
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	
	if (value == HIGH) {
		GPIO_WriteBit(gpio_port, gpio_pin, Bit_SET); 
	} else {
		GPIO_WriteBit(gpio_port, gpio_pin, Bit_RESET);
	}
}

void turn_led_on(int pins)
{
int value1 = pins&0x10000;
int value2 = pins&0x01000;
int value3 = pins&0x00100;
int value4 = pins&0x00010;
int value5 = pins&0x00001;

if (value1 == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
if (value2 == HIGH) {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
if (value3 == HIGH) {
digitalWrite(11, HIGH);
} else {
digitalWrite(11, LOW);
}
if (value4 == HIGH) {
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
}
if (value5 == HIGH) {
digitalWrite(9, HIGH);
} else {
digitalWrite(9, LOW);
}
}


/**
* @brief	Pauses the program for the amount of time (in miliseconds) specified as parameter. 
* @param	ms		the number of milliseconds to pause
* @retval	None
*/
void delay(uint32_t ms)
{
	uint32_t start = msTick;
	while ((msTick - start) < ms);
}


/**
* @brief	Returns the number of milliseconds since the board began running the current program. 
* @param	None
* @retval	Number of milliseconds since the program started
* @remark	This number will overflow (go back to zero), after approximately 50 days.
*/
uint32_t millis(void)
{
	return msTick;
}
